package com.lizhi.rpc.model;

import com.lizhi.utils.date.DateUtils;
import com.lizhi.yun.db.sfrobot.entity.RobotStatus;
import com.lizhi.yun.db.sfrobot.repository.RobotDeviceRepo;
import org.springframework.data.domain.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/11/20 11:44
 */
public class RobotStatusManager {

    public static RobotStatusManager Instance;

    private final static Object InstanceLockObj = new Object();

    public static RobotStatusManager getInstance() {
        if(Instance == null) {
            synchronized (InstanceLockObj) {
                if(Instance == null) {
                    Instance = new RobotStatusManager();
                    return Instance;
                }
            }
        }
        return Instance;
    }

    private final Object robotsLockObj = new Object();

    private List<RobotStatus> robots;

    public List<RobotStatus> getRobots(){
        if (this.robots == null){
            return new ArrayList<>();
        }
        for (int i = 0; i < this.robots.size(); i++){
            RobotStatus robotStatus = this.robots.get(i);
            if (robotStatus.getActiveTime() == null){
                this.robots.remove(i);
            }else if(DateUtils.getDiffSeconds(new Date(), robotStatus.getActiveTime()) > 600) {
                this.robots.remove(i);
            }
        }
        return this.robots;
    }

    public void init(List<RobotStatus> robotStatuses) {
        synchronized (this.robotsLockObj) {
            this.robots = new ArrayList<>();
            for (RobotStatus robotStatus : robotStatuses) {
                this.robots.add(robotStatus);
            }
        }
    }

    public void add(RobotStatus robotStatus) {
        if (this.robots == null){
            this.robots = new ArrayList<>();
        }
        synchronized (this.robotsLockObj) {
            for(RobotStatus robot : this.robots) {
                if(robot.getRobotDeviceId().equals(robotStatus.getRobotDeviceId())
                        && robot.getHospitalId().equals(robotStatus.getHospitalId())
                        && robot.getHospitalSystemId().equals(robotStatus.getHospitalSystemId())) {
                    return;
                }
            }
            robotStatus.setActiveTime(new Date());
            this.robots.add(robotStatus);
        }
    }

    public void updateStatus(RobotStatus robotStatus) {
        boolean isExist = false;
        if (this.robots == null){
            this.robots = new ArrayList<>();
        }
        for(RobotStatus robot : this.robots) {
            if(robot.getRobotDeviceId().equals(robotStatus.getRobotDeviceId()) && robot.getHospitalId().equals(robotStatus.getHospitalId()) && robot.getHospitalSystemId().equals(robotStatus.getHospitalSystemId())) {
                isExist = true;
                robot.setOnline(robotStatus.online);
                robot.setDataUpTime(robotStatus.getDataUpTime());
                robot.setActiveTime(new Date());
                if (robotStatus.positions != null) {
                    robot.positions = robotStatus.positions;
                }
                break;
            }
        }
        if(!isExist) {
            synchronized (this.robotsLockObj) {
                robotStatus.setActiveTime(new Date());
                this.robots.add(robotStatus);
            }
        }
    }
}
